﻿using Autofac.Features.OwnedInstances;
using DFGJ.WMS_PDA.WebAPI.Extensions;
using DFGK.WMS.Common.Extensions;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Models.PDA;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NetTaste;
using NLog.Fluent;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using System.Security.Claims;
using System.Transactions;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    /// <summary>
    /// PDA出库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PDAOutboundController : ControllerBase
    {

        private readonly DFGK_DB dbContext;
        private ITOutboundOrderDetailRepository TOutboundOrderDetail;
        private IPDAStackingRepository StackingRepository;
        private readonly ILogRepository logRepository;

        public PDAOutboundController(DFGK_DB dbContext, ITOutboundOrderDetailRepository TOutboundOrderDetail, IPDAStackingRepository StackingRepository, ILogRepository logRepository)
        {
            this.dbContext = dbContext;
            this.TOutboundOrderDetail = TOutboundOrderDetail;
            this.StackingRepository = StackingRepository;
            this.logRepository = logRepository;
        }

        #region 获取出库单明细Plus
        /// <summary>
        /// 获取出库单明细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTShippingOrderDetailPlus(string? key,int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalcount = 0;
            var list = TOutboundOrderDetail.GetTShippingOrderDetailPlus(key, pageIndex, pageSize,out totalcount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200",totalcount));

            }

            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }
        #endregion

        #region 出库复核
        /// <summary>
        /// 出库复核
        /// </summary>
        /// <param name="orderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitOutboudOrder(List<SubmitOutListDTO> orderDTO)
        {
            #region 库存数据修改代码
            /*
           int status = TOutboundOrderDetail.GetStatus(orderDTO.InventoryId);
           if (status != 4)
           {
               return Ok(BaseResult.Result("状态有误，请检查库存状态是否处于出库在途状态", "500"));
           }
           using (var tran = dbContext.Database.BeginTransaction())
           {

             try
             {


                 // 执行修改操作
                 // 1. 修改仓位状态
                  bool updateCellStatus = TOutboundOrderDetail.UpdateFullStatus(orderDTO.CellCode);
                 if (!updateCellStatus)
                 {
                     tran.Rollback();
                     return Ok(BaseResult.Result("修改仓位状态失败", "500"));
                 }

                 // 2. 修改库区状态
                 bool updateAreaStatus = TOutboundOrderDetail.UpdateAreasStatus(orderDTO.AreaCode);
                 if (!updateAreaStatus)
                 {
                     tran.Rollback();
                     return Ok(BaseResult.Result("修改库区状态失败", "500"));
                 }


                 // 获取库存数量
                 var qty = TOutboundOrderDetail.GetInventoryQuantity(orderDTO.MaterialsId);

                 if (qty < 0)
                 {
                     tran.Rollback();
                     return Ok(BaseResult.Result("库存数量小于0", "500"));
                 }

                 // 库存数量减去需要出库的数量
                 if (qty - orderDTO.Qty <= 0)
                 {
                     // 3. 库存表中删除数据
                     bool deleteInvenrory = TOutboundOrderDetail.DelTInventoriesById(orderDTO.InventoryId);
                     if (!deleteInvenrory)
                     {
                         tran.Rollback();
                         return Ok(BaseResult.Result("删除库存表失败", "500"));
                     }
                     return Ok(BaseResult.Result("删除库存成功","200"));
                 }
                 // 减去库存数量并修改库存状态
                 if (!TOutboundOrderDetail.GetDwindleNumbers(orderDTO.InventoryId, orderDTO.Qty))
                 {
                     tran.Rollback();
                     return Ok(BaseResult.Result("库存数据删除失败", "500"));
                 }

                 // 往库存流水表添加数据
                 bool AddInventoryFlow = TOutboundOrderDetail.AddInventoryFlow(orderDTO);
                 if (!AddInventoryFlow)
                 {
                     tran.Rollback();
                     return Ok(BaseResult.Result("库存流水信息插入失败", "500"));
                 }

                 //提交事务
                 tran.Commit();
                 return Ok(BaseResult.Result("库存减少成功！", "200"));
             }
             catch (Exception ex)
             {
                 // 发生异常，回滚事务
                 tran.Rollback();
                 throw;
             }
                 
          */

            #endregion
            string message;
            if (orderDTO == null)
            {
                return Ok(BaseResult.Result("400", "参数传入错误！"));
            }
            string uname = HttpContext.User.FindFirstValue("userName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("id"));
            string runame = HttpContext.User.FindFirstValue("realName");
            string selecode = HttpContext.User.FindFirstValue("selectPositionCode");
            using (var tran = dbContext.Database.BeginTransaction())
            {
                try
                {
                    foreach (var item2 in orderDTO)
                    {
                        bool result = false;
                        item2.createUserld = uid;
                        item2.createUser = uname;
                        item2.nowUserRealName = runame;
                        item2.NowLoginUserId = uid;
                        item2.StationCode = selecode;
                        if(string.IsNullOrEmpty(item2.StationCode)|| item2.StationCode.Length <= 0)
                        {
                            return Ok(BaseResult.Result("400", "站台编码有误，请检査请求参数"));
                        }
                        result = TOutboundOrderDetail.SubmitOutInventoryReview(item2, out message);

                        if (!result)
                        {
                            //transaction.Dispose();
                            tran.Rollback();
                            return Ok(BaseResult.Result("500", message));
                        }

                        if (TOutboundOrderDetail.GetStackingStorages(item2.palletBarcode).Count <= 0)
                        {
                            tran.Rollback();
                            return Ok(BaseResult.Result("500", "请求参数有误，请检查请求参数"));
                        }

                        //根据垛条码查询垛主
                        var item = TOutboundOrderDetail.GetStackingStacker(item2.palletBarcode);
                        if (item.Count == 0)
                        {
                            //transaction.Dispose();
                            tran.Rollback();
                            return Ok(BaseResult.Result("500", "该托条码对应的垛主信息有误"));
                        }

                        // 根据垛条码删除入库垛信息表中数据
                        if (!TOutboundOrderDetail.HandleUnbindOfOutstorage(item2, out message))
                        {
                            //transaction.Dispose();
                            tran.Rollback();
                            return Ok(BaseResult.Result("500", message));
                        }

                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    // 回滚事务（如果发生异常）
                    tran.Rollback();
                    //transaction.Dispose(); // 或者使用 tran.Rollback()
                    return Ok(BaseResult.Result("500", ex.Message));
                }
                //}

                return Ok(BaseResult.Result("200", "出库下架完成"));
            }
        }
        #endregion

        #region 出库复核 弃用
        /// <summary>
        /// 出库复核
        /// </summary>
        /// <param name="orderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitOutboudOrderPlus(SubmitOutListDTO orderDTO)
        {
            string message;
            if (orderDTO == null)
            {
                return Ok(BaseResult.Result("500", "参数传入错误！"));
            }

            using (var tran = dbContext.Database.BeginTransaction())
            {
                try
                {
                    bool result = false;
                    result = TOutboundOrderDetail.SubmitOutInventoryReviewPlus(orderDTO, out message);

                    if (!result)
                    {
                        //transaction.Dispose();
                        tran.Rollback();
                        return Ok(BaseResult.Result("500", message));
                    }

                    if (TOutboundOrderDetail.GetStackingStorages(orderDTO.palletBarcode).Count <= 0)
                    {
                        tran.Rollback();
                        return Ok(BaseResult.Result("500", "请求参数有误，请检查请求参数"));
                    }

                    //根据垛条码查询垛主
                    var item = TOutboundOrderDetail.GetStackingStacker(orderDTO.palletBarcode);
                    if (item.Count == 0)
                    {
                        //transaction.Dispose();
                        tran.Rollback();
                        return Ok(BaseResult.Result("500", "该托条码对应的垛主信息有误"));
                    }

                    // 根据垛条码删除入库垛信息表中数据
                    if (!TOutboundOrderDetail.HandleUnbindOfOutstoragePlus(orderDTO, out message))
                    {
                        //transaction.Dispose();
                        tran.Rollback();
                        return Ok(BaseResult.Result("500", message));
                    }

                    // 提交事务
                    //transaction.Complete();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    // 回滚事务（如果发生异常）
                    tran.Rollback();
                    //transaction.Dispose(); // 或者使用 tran.Rollback()
                    return Ok(BaseResult.Result("500", ex.Message));
                }
                //}

                return Ok(BaseResult.Result("200", "出库解绑成功"));
            }
        }
        #endregion

        #region 根据出库单id获取出库单详细
        /// <summary>
        /// 根据出库单id获取出库单详细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPalletBarcodePDA(int OrderDetailId)
        {
            var pallet = TOutboundOrderDetail.GetPalletBarcodePDA(OrderDetailId);
            if (pallet.Count <= 0 && pallet == null)
            {
                return Ok(BaseResult.Result("出库解绑失败", "500"));
            }
            return Ok(BaseResult.Result(pallet, "200"));
        }
        #endregion

        /// <summary>
        /// 出库现用
        /// </summary>
        /// <param name="orderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult OutWareHouse(SubmitOutListDTONow orderDTO)
        {
            string message;
            try
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                orderDTO.CreateUser = uname;
                orderDTO.CreateUserld = uid;
                orderDTO.NowUserRealName = runame;
                var resOut = TOutboundOrderDetail.ChukuMsg(orderDTO, out message, out bool isOutOver);
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog
                {
                    LogType = 1,
                    ActionType = 5,
                    ModuleName = "出库管理",
                    UserName = uname,
                    RealName = runame,
                    UserId = uid,
                    Description = resOut ? uname + message + ",托盘：" + orderDTO.PalletBarcode[0] : uname + "出库失败：" + message + ",托盘：" + orderDTO.PalletBarcode[0],
                    Ipaddress = ip,
                    Status = resOut ? 1 : 0
                };
                var logAddRes = logRepository.AddTLod(logg);
                if (resOut)
                {
                    if (isOutOver)
                    {
                        return Ok(BaseResult.Result(true, message, "200"));
                    }
                    return Ok(BaseResult.Result("200", message));
                }
                else
                {
                    if (!logAddRes)
                    {
                        return Ok(BaseResult.Result("500", "日志添加失败"));
                    }
                    return Ok(BaseResult.Result("500", message));
                }
            }
            catch (Exception ex)
            {
                logRepository.Add(new TLog()
                {
                    ActionType = 0,
                    CreateDate = DateTime.Now,
                    Description = ex.Message,
                    LogType = 1,
                });
                return Ok(BaseResult.Result("500", ex.Message));
            }

        }


        /// <summary>
        /// 出库现用  添加移库
        /// </summary>
        /// <param name="orderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult OutWareHouseAndMove(SubmitOutListDTONow orderDTO)
        {
            string message;
            try
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                orderDTO.CreateUser = uname;
                orderDTO.CreateUserld = uid;
                orderDTO.NowUserRealName = runame;
                var resOut = TOutboundOrderDetail.OutHouse(orderDTO, out message, out bool isOutOver);
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog
                {
                    LogType = 1,
                    ActionType = 5,
                    ModuleName = "出库管理",
                    UserName = uname,
                    RealName = runame,
                    UserId = uid,
                    Description = resOut ? uname + message + ",托盘：" + orderDTO.PalletBarcode[0] : uname + "出库失败：" + message + ",托盘：" + orderDTO.PalletBarcode[0],
                    Ipaddress = ip,
                    Status = resOut ? 1 : 0
                };
                var logAddRes = logRepository.AddTLod(logg);
                if (resOut)
                {
                    if (isOutOver)
                    {
                        return Ok(BaseResult.Result(true, message, "200"));
                    }
                    return Ok(BaseResult.Result("200", message));
                }
                else
                {
                    if (!logAddRes)
                    {
                        return Ok(BaseResult.Result("500", "日志添加失败"));
                    }
                    return Ok(BaseResult.Result("500", message));
                }
            }
            catch (Exception ex)
            {
                logRepository.Add(new TLog()
                {
                    ActionType = 0,
                    CreateDate = DateTime.Now,
                    Description = ex.Message,
                    LogType = 1,
                });
                return Ok(BaseResult.Result("500", ex.Message));
            }

        }      
    }

    #region 废弃


    /* 
    #region 根据ID获取出库单明细详细
    /// <summary>
    /// 根据ID获取出库单明细详细
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public IActionResult GetTShippingOrderDetailById(int id)
    {
        var list = TOutboundOrderDetail.GetTShippingOrderDetailById(id);
        if (list != null)
        {
            return Ok(BaseResult.Result(list, "查询成功", "200"));
        }
        return Ok(BaseResult.Result(null, "查询失败", "500"));
    }
    #endregion
    */

    /*
    #region 根据出库单号获取出库单详细数据
    /// <summary>
    /// 根据出库单号获取出库单详细数据
    /// </summary>
    /// <param name="OrderSn"></param>
    /// <returns></returns>
    [HttpGet]
    public IActionResult GetDeliveryOrderDetails(string OrderSn)
    {
        var list = TOutboundOrderDetail.GetDeliveryOrderDetails(OrderSn);
        if (list != null)
        {
            return Ok(BaseResult.Result(list, "查询成功", "200"));
        }
        return Ok(BaseResult.Result(null, "查询失败", "500"));
    }
    #endregion

    #region 拣选出库
    /// <summary>
    /// 拣选出库
    /// </summary>
    /// <param name="item"></param>
    /// <param name="itemDetail"></param>
    /// <returns></returns>
    [HttpPost]
    public IActionResult CreatePickingOutbound(TPickingStationDatum item)
    {
        using (var tran = dbContext.Database.BeginTransaction())
        {
            try
            {
                if (item == null)
                {
                    return Ok(BaseResult.Result("请求参数有误，请重新输入参数", "500"));
                }
                var list = TOutboundOrderDetail.AddPickingOutbound(item);
                if (!list)
                {
                    tran.Rollback();
                    return Ok(BaseResult.Result("出库信息不能为空", "500"));
                }

                tran.Commit();
                return Ok(BaseResult.Result(list, "数据已添加至拣选出库中", "200"));
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw;
            }
        }
    }
    #endregion

    #region 获取拣选出库的信息
    /// <summary>
    /// 获取拣选出库的信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public IActionResult GetPickingStation(int id)
    {
        var list = TOutboundOrderDetail.GetPickingStationDatum(id);
        if (list == null)
        {
            return Ok(BaseResult.Result("未查询到数据", "500"));
        }
        int PickingStationDataId = -1;
        foreach (var Pid in list)
        {
            PickingStationDataId = Pid.Id;
        }
        if (PickingStationDataId <= -1)
        {
            return Ok(BaseResult.Result("未查询到详情数据", "500"));
        }
        var item = TOutboundOrderDetail.GetPickingStationDetail(PickingStationDataId);

        var result = new
        {
            List = list,
            Item = item
        };

        return Ok(BaseResult.Result(result, "查询成功", "200"));
    }
    #endregion

    #region 提交出库单执行
    /// <summary>
    /// 提交出库单执行
    /// </summary>
    /// <param name="OrderDTO"></param>
    /// <returns></returns>
    [HttpPost]
    public IActionResult SubmitOutboundOrder(CreateOutboundOrderDTO OrderDTO)
    {
        bool result = false;
        // 查询出库单详情信息
        TShippingOrderDetail shippingOrderDetail = TOutboundOrderDetail.GetTShippingOrderDetailById(OrderDTO.OrderRunInputModel.OrderId);
        if (shippingOrderDetail == null)
        {
            {
                return Ok(BaseResult.Result("库存详情查询失败", "500"));
            }
        }

        List<TStation> inAndOutPortStations = TOutboundOrderDetail.GetStation(OrderDTO.WareHouseId);

        // 判断是否有出库站台
        if (inAndOutPortStations == null || inAndOutPortStations.Count == 0)
        {
            return Ok(BaseResult.Result("未找到可用的出入口站台！", "500"));
        }

        List<CmdDeviceModeModel> deviceModeList = inAndOutPortStations.Select(t => new CmdDeviceModeModel
        {
            InAndOutPort = t.Code,
            Mode = 2,
        }).ToList();
        // 判断物料是否存在
        if (TOutboundOrderDetail.GetMaterial(OrderDTO.MaterialsId) == null)
        {
            return Ok(BaseResult.Result("填写的物料信息不存在，请重新填写！", "500"));
        }
        // 判断出库数量是否正确
        if (OrderDTO.Qty < 0)
        {
            return Ok(BaseResult.Result("出库数量填写无效，请填写正确的值！", "500"));
        }
        // 判断出库数量的值是否满足库存现有的值
        if (TOutboundOrderDetail.GetInventoryQty(OrderDTO.MaterialsId) <= 0)
        {
            return Ok(BaseResult.Result("库存数量不满足出库数量，请重新填写", "500"));
        }

        //根据库存Id查询库存表中是否存在这条数据
        if (TOutboundOrderDetail.GetInventory(OrderDTO.InventoryId) == 0)
        {
            return Ok(BaseResult.Result("库存中不存在此条数据，请检查数据有效性", "500"));
        }
        // 判断仓位编码是否存在
        if (OrderDTO.CellCode == null)
        {
            return Ok(BaseResult.Result("仓位编码不存在，请检查仓位编码是否正确", "500"));
        }
        // 获取出库库区是否存在
        //if (OrderDTO.AreaCode == null)
        //{
        //    return Ok(BaseResult.Result("库区编码不存在，请检查库区编码是否正确", "500"));
        // }

        // 执行
        string message;
        bool updateStatus = TOutboundOrderDetail.RunShippingOrderDetail(OrderDTO.OrderRunInputModel.OrderId, shippingOrderDetail,out message);
        if (!updateStatus)
        {
            return Ok(BaseResult.Result(message, "500"));
        }


        return Ok(BaseResult.Result("出库单提交成功！", "200"));
    }
    #endregion
    #endregion

      /// <summary>
        /// 查询出库单参数相关数据
        /// </summary>
        /// <param name="OrderSn"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOutboundOrder(string PalletBarcode)
        {
            var list = TOutboundOrderDetail.GetOutboundOrder(PalletBarcode);
            if (PalletBarcode == null)
            {
                return Ok(BaseResult.Result(null, "请求参数有误", "500"));
            }

            return Ok(BaseResult.Result(list, "请求成功", "200"));

        }

       /*
        #region 组垛

        /// <summary>
        /// 提交垛理货数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitPalletTallyData(TPalletTallyInputModel data)
        {
            bool result = true;
            if (data.OrderDetailId.GetValueOrDefault() <= 0 || string.IsNullOrEmpty(data.PalletBarcode) || data.PalletPackage == null)
            {
                return Ok(BaseResult.Result("请求参数有误", "500"));
            }
            if (string.IsNullOrWhiteSpace(data.LogicPartCode))
            {
                return Ok(BaseResult.Result("请选择逻辑分区", "500"));
            }

            List<string> PalletBarcodeList = data.PalletBarcode.StringToList();
            if (PalletBarcodeList.Count == 1)
            {
                var Pallet = StackingRepository.GetPallet(new TPalletCriteriaModel() { PalletBarcode = data.PalletBarcode });
                if (Pallet == null)
                {
                    return Ok(BaseResult.Result("参数错误", "500"));
                }
                List<TDictItem> dictItems = StackingRepository.GetDictItemListByTypeCode(data.LogicPartCode);
                if (Pallet.Type == (int)PalletTypeEnum.Pallet)
                {
                    result = data.LogicPartCode == "TuoPan";
                }
                else if (Pallet.Type == (int)PalletTypeEnum.Box64)
                {
                    result = data.LogicPartCode == "Box6040";
                }
                else if (Pallet.Type == (int)PalletTypeEnum.Box86)
                {
                    result = data.LogicPartCode == "Box8060";
                }
                if (!result)
                {
                    return Ok(BaseResult.Result("托盘条码与逻辑分区不匹配，请重新选择", "500"));
                }

                List<TStackingStorage> storageModels = StackingRepository.GetStackingStorageList(new TStackingStorageCriteriaModel()
                {
                    Status = 1,
                });
                if (storageModels != null && storageModels.Count == PalletBarcodeList.Count)
                {
                    return Ok(BaseResult.Result("托盘已在组垛中", "500"));
                }
                var list = StackingRepository.GetInventoryByPalletBarcode(data.PalletBarcode);
                if (list != null)
                {
                    return Ok(BaseResult.Result("当前托盘已在库存中", "500"));
                }

            }
            else
            {
                List<TStackingStorage> storageModels = StackingRepository.GetStackingStorageList(new TStackingStorageCriteriaModel()
                {
                    Status = 1,
                });
                if (storageModels != null && storageModels.Count == PalletBarcodeList.Count && storageModels.Any(t => !PalletBarcodeList.Contains(t.PalletBarcode)))
                {
                    return Ok(BaseResult.Result("托盘已在组垛中", "500"));
                }
                var list = StackingRepository.GetInventoryByPalletBarcode(data.PalletBarcode);
                if (list != null)
                {
                    return Ok(BaseResult.Result("当前托盘已在库存中", "500"));
                }
            }

            var orderDetail = StackingRepository.GetReceivingOrderDetail(data.OrderDetailId);
            if (orderDetail != null)
            {
                return Ok(BaseResult.Result("未找到入库单", "500"));
            }
            if (orderDetail.Status == 0 || orderDetail.Status == 1)
            {
                return Ok(BaseResult.Result("入库单未审核", "500"));
            }
            if (orderDetail.Status == 4 || orderDetail.Status == 5)
            {
                return Ok(BaseResult.Result("入库单已完成", "500"));
            }

            if (orderDetail.Status != 2 && orderDetail.Status != 3)
            {
                return Ok(BaseResult.Result("入库单状态有误", "500"));
            }
            decimal enabledNum = orderDetail.Qty.GetValueOrDefault() - orderDetail.StackedQty.GetValueOrDefault();
            if (enabledNum - data.TotalNum < 0)
            {
                return Ok(BaseResult.Result("数量超出，最多还能组" + enabledNum.ToString() + orderDetail.UnitName, "500"));
            }

            var material = StackingRepository.GetMaterial(data.PalletPackage.MaterialCode);
            if (material == null)
            {
                return Ok(BaseResult.Result("未找到物料数据", "500"));
            }
            if (material.Code != data.PalletPackage.MaterialCode)
            {
                return Ok(BaseResult.Result("包装信息中的物料与单据物料不一致", "500"));

            }
            if (material.IsDismantle.HasValue && !material.IsDismantle.Value && PalletBarcodeList.Count > 1)
            {
                return Ok(BaseResult.Result("非拆卸物料不允许扫描多个托盘", "500"));
            }
            DFGK_WMS.Model.Models.PDA.PalletBindMaterialModel palletBindMaterialModel = new DFGK_WMS.Model.Models.PDA.PalletBindMaterialModel()
            {
                PalletBarcode = data.PalletBarcode,
                LogicPartCode = data.LogicPartCode,
                MaterialPackageBindModel = new DFGK_WMS.Model.Models.PDA.MaterialPackageBindModel()
                {
                    OrderDetailId = data.OrderDetailId.Value,
                    MaterialCode = data.PalletPackage.MaterialCode,
                    Barcodes = data.PalletPackage.Barcodes,
                    Qty = data.TotalNum,
                }
            };





            return Ok(BaseResult.Result("提交垛埋货数据成功", "200"));
        }


            /*
        #region 出库解绑
        /// <summary>
        /// 出库解绑
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult OutboundUnbinding(string PalletBarcode)
        {
            if (TOutboundOrderDetail.GetStackingStorages(PalletBarcode).Count <= 0)
            {
                return Ok(BaseResult.Result("请求参数有误，请检查请求参数", "500"));
            }
            // 根据垛条码查询物料
            var list = TOutboundOrderDetail.GetMaterial(PalletBarcode);
            if (list.Count == 0)
            {
                return Ok(BaseResult.Result("该垛条码对应的托盘没有物料", "500"));
            }

            // 根据垛条码查询垛主
            var item = TOutboundOrderDetail.GetStackingStacker(PalletBarcode);
            if (item.Count == 0)
            {
                return Ok(BaseResult.Result("该托条码对应的垛主信息有误", "500"));
            }
            try
            {
                // 根据垛条码删除入库垛信息表中数据
                if (!TOutboundOrderDetail.HandleUnbindOfOutstorage(PalletBarcode))
                {
                    return Ok(BaseResult.Result("出库解绑失败", "500"));
                }

                // 提交事务
            }
            catch
            {
                throw;
            };
            return Ok(BaseResult.Result("出库解绑成功!", "200"));
        }

        #endregion*/
    #endregion


}
